package com.base.themvx.utils

import androidx.collection.ArrayMap

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * <P>DESC:
 * 事件上报数据组装
 * </p>
 * ******************(^_^)***********************
 */
class EventMap : ArrayMap<String, Any>() {

    companion object Builder {

        /**
         * 构建一个　[EventMap]
         */
        @JvmStatic
        fun eventMap(eventId: String): EventMap {
            return EventMap().apply {
                this.mEventId = eventId
            }
        }

        /**
         * 事件上报实现者
         */
        var mEventReporter: IEventReporter? = null
    }


    private var mEventId: String = ""

    fun withEventId(eventId: String) = apply { this.mEventId = eventId }

    fun with(key: String, value: Any?) = apply {
        put(key, value)
    }

//    /**
//     * 上报事件：具体的上报逻辑为　[trackBlock] 来执行
//     */
//    private fun trackMe(trackBlock: suspend (eventId: String, eventContent: String) -> Unit): EventMap {
//        if (mEventId.isBlank()) {
//            return this
//        }
//        XCoroutineScope.ioCoroutineScope?.launch {
//            val eventContent = GsonUtil.toJSON(this)
//            trackBlock(mEventId, eventContent)
//            clear()
//        }
//        return this
//    }

    /**
     * 上报事件：具体的上报逻辑由接口实现者来实现
     */
    fun trackMe(): EventMap {
        if (mEventId.isNotBlank()) {
//            val eventContent = GsonUtil.toJSON(this)
            mEventReporter?.onEventReport(this)
        }
        return this
    }

    fun getEventId() = mEventId
}